<template>
    <div class="box_">
        <div class="info">
          <div class="info_">  Name: <input type="text" placeholder="必填" class="qq" v-model="name" maxlength="20"></div>
          <div class="info_">QQ: <input type="text" placeholder="必填" class="qq" v-model="auto" maxlength="20"></div>
          <div class="info_">Email: <input type="text" placeholder="选填" class="qq" v-model="email" maxlength="20"></div>
        </div>
        <div class="box_content">
            <textarea contenteditable="true" class="content"  ref="edit" @focus="ess" @blur="ess_b"></textarea>
        </div>

        <div class="function">
            <div class="choose">
                <span @click="exp_s" >
                    <img src="/img/common/biaoqing.png" alt="" title="表情" style="width:30px">
                </span>
                <button @click="send">发送评论</button>
            </div>
            <div class="expression" v-show="expression" >
                <ul >
                    <li @click="expression_li($event)" v-for="item in expression_e" v-html="item"></li>
                </ul>
            </div>
        </div>

    </div>
</template>



<script >
import api from "@/api";

export default {
    name: "MessageWordPad",
    data(){
        return{
            expression:false,
            content:"勿喷，谢谢配合。",
            messageSend:{
                auto:"",
                name:"",
                email:"",
                content:""
            },
            bulidIndex:0,
            auto:"",
            name:"",
            email:"",
            expression_e:['😀', '😄', '😁', '😅', '🤣', '🙂', '😉', '😊', '🥰', '😍', '😘', '🤑',
                '😚', '😑', '😏', '😪', '😎', '😾', '😭', '😓', '😖', '👋', '🤬', '😷',
                '🤒', '🤕', '🤢', '🤮', '🤧', '😇', '🤠', '🤡', '🤥', '🤫', '🤭', '🧐',
                '🤓', '😈', '👿', '👹', '👺', '💀', '👻', '👽', '🤖', '💣', '💩', '😺',
                '😸', '😹', '😻', '😼', '😽', '🙀', '😿', ]
        }
    },
    mounted() {

    },
    watch:{

    },
    methods: {
        expression_li(e){
            let text =  this.$refs.edit.value;
            this.$refs.edit.value= text.slice(0, this.bulidIndex) + e.target.innerText + text.slice(this.bulidIndex);
        },
        ess_b(e){
            this.bulidIndex = e.target.selectionStart;
        },
        ess() {
            if(this.$refs.edit.value === "")
                this.$refs.edit.value = "";
        },
        exp_s() {
            this.expression = !this.expression;
        },
        send() {
            //更新第一页的评论
            if(this.$refs.edit.value === "" ){
                alert("回复内容不能为空！")
                return;
            }
            if(this.$refs.edit.value.length>500){
                alert("最多可输入200个字！")
                return;
            }
            if(!(/^[1-9]\d{4,9}$/.test(String(this.auto)))){
                alert("无效QQ！")
                return;
            }
            if(this.name === ""){
                alert('名字不能为空')
                return;
            }
            let email = /^([a-zA-Z\d][\w-]{2,})@(\w{2,})\.([a-z]{2,})(\.[a-z]{2,})?$/ ;
            if(!email.test(this.email) && this.email !==""){
                alert('邮箱格式错误！')
                return;
            }
            this.messageSend.content = this.$refs.edit.value;
            this.messageSend.auto = this.auto;
            this.messageSend.name = this.name;
            this.messageSend.email= this.email;
            this.messageSend.system = localStorage.getItem("platform")
            this.$emit("handleCommentAdd",this.messageSend)
            api.MessageInsert(this.messageSend).then((res)=>{
                if(res.data.code === 200){
                    alert("发表成功")
                }else {
                    alert("网络繁忙，请稍后再试")
                }
            })
          setTimeout(()=>{
              this.$refs.edit.value = "";
              this.expression = false;
              this.name = "";
              this.auto=""
              this.email=""
          },1000)
        },
    }
}
</script>



<style scoped>
.box_{
    padding: 10px;
    border-radius: 3px;
}
.content{
    width: 100%;
    height: 150px;
    overflow: auto;
    color: #838080;
    padding: 10px;
    outline: none;
    resize: none;
    word-break: break-all;
    border: 1px solid #8f8c8c;
}

.box_content{
    width: 100%;
    padding: 10px;
    border-top-right-radius: 5px;
    border-top-left-radius: 5px;
}
.function{
    width: 100%;
    padding: 0 20px;

}
.choose{
    display: flex;
    justify-content: space-between;
    align-items: center;

}
button{
    padding: 7px 13px;
    background: #3586f1;
    border-radius: 3px;
    border:0;
    color: white;
}
button:hover{
    opacity: .9;
}
button:hover,img:hover{
    cursor: pointer;
}
.expression{
    width: 100%;
    margin-top: 10px;
    padding: 5px;
    animation: box .5s ease-in-out 0s 1 forwards;
    border-radius: 4px;
}
.expression ul{
    display: flex;
    flex-flow:row wrap;
    justify-content: space-around;
    align-items: center;

}
.expression ul li{
    list-style: none;
    font-size: 28px;
}
.expression ul li:last-child{
    margin-right: auto !important;
}
.expression ul li:hover{
    cursor: pointer;
}
@keyframes box {
    0%{
        transform: scale(0);
    }
    70%{
        transform: scale(1.1);
    }
    100%{
        transform: scale(1);
    }
}
.qq{
    height: 30px;
    outline: none;
    color: #8f8c8c;
    margin-bottom: 5px;
    width: 180px;
    border: 0;
    text-indent: 0.5rem;
    border-bottom: 1px solid;
    background: transparent;
}
.info{
    display: flex;
    justify-content: space-around;
    flex-direction: row;
}
.info_{
    width: 250px;
    display: flex;
    justify-content: space-between;
    flex-direction: row;
}
@media (min-width: 0) and (max-width: 800px) {
    .info{
        display: flex;
        flex-direction: column;
    }
}

</style>